<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Sites extends Model
{
    //
    protected $table = 'sites';
    public $timestamps = false;     // 自动维护时间
    protected $dateFormat = 'U';       // 自动维护时间格式为时间戳
//    protected $fillable=[];             // 字段白名单（和黑名单二选一，只出现一个）
    protected $guarded = [];            // 去除黑名单

//    关联category
    public function categorys()
    {
        return $this->belongsToMany(Category::class, 'sites_category');
    }

//    关联开头结尾startends
    public function startends()
    {
        return $this->hasMany(Startend::class);
    }

//    关联模版
    public function templates()
    {
        return $this->hasMany(Template::class);
    }

//    关联发送队列
    public function sendlists()
    {
        return $this->hasMany(Sendlist::class);
    }

//    关联默认模版
    public function getTemplateAttribute()
    {
        $record = $this->templates->where('is_use', 1)->first();
        return $record->template;
    }

//    获取站点关联的分类id   category_ids
    public function getIdsAttribute()
    {
        $ids = $this->categorys()->pluck('category_id');
        $ids = substr($ids, 1, -1);
        return $ids;
    }

}
